package clientView;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;

import clientUtilGUI.UtilityGUI;

public class Spe_View extends JPanel implements ActionListener {
	
	private JPanel panelHeader;
	private JPanel panelBody;
	private GridBagLayout gbl_panelBody;
	private JButton btnAdd;
	public static JTable tblSpe;
	public static JScrollPane scrollPane;
	private JPanel panelPage;
	private JButton btnBack;
	private JButton btnNext;
	private JComboBox cbbPage;
	private int pageCur = 1;
	public Spe_View() {	
		setLayout(new BorderLayout(0, 0));
		panelHeader = UtilityGUI.headerForm("Danh Sách Chuyên Nghành");
		add(panelHeader, BorderLayout.NORTH);

		panelBody = new JPanel();
		gbl_panelBody = new GridBagLayout();
		gbl_panelBody.columnWidths = new int[]{0, 0, 0};
		gbl_panelBody.rowHeights = new int[]{0, 0, 0};
		gbl_panelBody.columnWeights = new double[]{1.0, 0.0, 0.0};
		gbl_panelBody.rowWeights = new double[]{0.0, 1.0, 0.0};
		panelBody.setLayout(gbl_panelBody);
		add(panelBody, BorderLayout.CENTER);

		btnAdd = new JButton("Thêm Chuyên Nghành");
		btnAdd.setFont(UtilityGUI.fontBtnForm);
		btnAdd.addActionListener(this);
		GridBagConstraints gbc_btnAdd = new GridBagConstraints();
		gbc_btnAdd.fill = GridBagConstraints.BOTH;
		gbc_btnAdd.insets = new Insets(10, 0, 5, 10);
		gbc_btnAdd.gridx = 2;
		gbc_btnAdd.gridy = 0;
		panelBody.add(btnAdd, gbc_btnAdd);
		
		tblSpe = UtilityGUI.getTable("Specialized", pageCur, "Specialized_Id", true);
		tblSpe.addMouseListener(new Mouse_tblSpe());

		scrollPane = new JScrollPane();
		GridBagConstraints gbc_scrollPane = new GridBagConstraints();
		gbc_scrollPane.insets = new Insets(0, 10, 5, 10);
		gbc_scrollPane.fill = GridBagConstraints.BOTH;
		gbc_scrollPane.gridwidth = 3;
		gbc_scrollPane.gridx = 0;
		gbc_scrollPane.gridy = 1;
		scrollPane.setViewportView(tblSpe);
		panelBody.add(scrollPane, gbc_scrollPane);
		
		panelPage = new JPanel();
		GridBagConstraints gbc_panelPage = new GridBagConstraints();
		gbc_panelPage.gridwidth = 3;
		gbc_panelPage.insets = new Insets(0, 10, 10, 10);
		gbc_panelPage.fill = GridBagConstraints.BOTH;
		gbc_panelPage.gridx = 0;
		gbc_panelPage.gridy = 2;
		panelBody.add(panelPage, gbc_panelPage);
		panelPage.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
		
		btnBack = new JButton(new ImageIcon("src/image/back.png"));
		btnBack.addActionListener(this);
		panelPage.add(btnBack);
		
		cbbPage = UtilityGUI.cbbPaging("Specialized");
		cbbPage.addActionListener(this);
		panelPage.add(cbbPage);
		
		btnNext = new JButton(new ImageIcon("src/image/next.png"));
		btnNext.addActionListener(this);
		panelPage.add(btnNext);
		
	}
	
	public static class Mouse_tblSpe  extends MouseAdapter{
		public void mouseClicked(MouseEvent e) {
			if((e.getModifiers() & InputEvent.BUTTON1_MASK) != 0){
				int row = tblSpe.getSelectedRow();
				int col = tblSpe.getSelectedColumn();
				if(col == tblSpe.getColumnCount()-1){
					String name =	(String) tblSpe.getModel().getValueAt(row, 1);
					String des =	(String) tblSpe.getModel().getValueAt(row, 2);
					int id  = (Integer) tblSpe.getModel().getValueAt(row, 0);
					new Spe_Update(null,name,des,id,row).setVisible(true);
				}
			}
		}
	}

	public static Mouse_tblSpe getMouse_tblSpe(){
		return getMouse_tblSpe();
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == btnAdd){
		   new Spe_Insert(null).setVisible(true);
		}else if(e.getSource() == btnBack){
			if(pageCur == 1){
				return;
			}
			tblSpe.setVisible(false);
			pageCur = pageCur - 1;
			cbbPage.setSelectedItem(pageCur);
			tblSpe = UtilityGUI.getTable("Specialized", pageCur, "Specialized_Id", true);
			tblSpe.addMouseListener(new Mouse_tblSpe());
			scrollPane.setViewportView(tblSpe);
		}else if(e.getSource() == btnNext){
			if(pageCur == cbbPage.getItemCount()){
				return;
			}
			tblSpe.setVisible(false);
			pageCur = pageCur + 1;
			cbbPage.setSelectedItem(pageCur);
			tblSpe = UtilityGUI.getTable("Specialized", pageCur, "Specialized_Id", true);
			tblSpe.addMouseListener(new Mouse_tblSpe());
			scrollPane.setViewportView(tblSpe);
		}else if(e.getSource() == cbbPage){
			int pageGo = (Integer) cbbPage.getSelectedItem();
			if(pageCur == pageGo){
				return;
			}else{
				pageCur = pageGo;
				tblSpe.setVisible(false);
				tblSpe = UtilityGUI.getTable("Position", pageGo, "Pos_Id", true);
				tblSpe.addMouseListener(new Mouse_tblSpe());
				scrollPane.setViewportView(tblSpe);
			}
		}
		
	}
	
}

